/***
author: sailing
date: 2018/4/27
****/
import dataC from './data'
import Config from './config'
import cyMrg from './cyMrg'


function getPosition_ (nodeA, nodeB) {
	var AP = nodeA.position()
	var BP = nodeB.position()
	var ah = nodeA.height()
	var bh = nodeB.height()
    
	var direction = jdirection(nodeA, nodeB)

    var centerH = Math.abs(BP.y - AP.y) - ah/2 - bh/2
    var y = 0
    if(AP.y > BP.y) {
    	y = BP.y + bh/2 + centerH/2
    }else {
    	y = AP.y + ah/2 + centerH/2
    }


    var nodeAPos = {
    	x: AP.x,
    	y: y
    }

    var nodeBPos = {
    	x: BP.x,
    	y: y
    }

    return [nodeAPos, nodeBPos]

}

function getPosition (nodeA, nodeB) {
	var AP = nodeA.position()
	var BP = nodeB.position()
	var ah = nodeA.height()
	var bh = nodeB.height()
    var ch = 100
	var direction = jdirection(nodeA, nodeB)

    var centerH = Math.abs(BP.y - AP.y) - ah/2 - bh/2
    var y = 0

   if(AP.y > BP.y) {
    	y = BP.y + bh/2 + ch
    }else {
    	y = AP.y + ah/2 + ch
    }

    


    var nodeAPos = {
    	x: AP.x,
    	y: y
    }

    var nodeBPos = {
    	x: BP.x,
    	y: y
    }

    return [nodeAPos, nodeBPos]

}

function findLine (arr , id) {
	for(var i=0; i< arr.length; i++) {
		var edge = arr[i].data()
		if(edge.id != id) return arr[i]
	}
}


function jdirection (nodeA, nodeB) {
	var AP = nodeA.position()
	var BP = nodeB.position()
	var ah = nodeA.height()
	var bh = nodeB.height()
	var aw = nodeA.width()
	var bw = nodeB.width()

	var bt = {
		y: BP.y - bh/2,
		x: AP.x
	}

	var bb = {
		y: BP.y + bh/2,
		y: AP.x
	}

	if (AP.y < BP.y) {
		return 't'
	} else if (AP.y > bt.y && AP.y <bb.y) {
		if (AP.x < BP.x) {
			return 'l'
		}else {
			return 'r'
		}
	}else {
		return 'b'
	}
    
}

function findNodeArr (nodeA, line) {
	var arr = [nodeA];
	var edgeLine = cyMrg.findIdLine(line.id)
	var newNodeA = edgeLine.target()

	arr.push(newNodeA)

	var lineArr = newNodeA.connectedEdges()
    
	var cline = findLine(lineArr, line.id)

	var clineData = cline.data()

	var newNodeB = ''
	if (clineData.target == newNodeA.data().id) {
		newNodeB = cline.source()
		arr.push(newNodeB)
	}else {
		newNodeB = cline.target()
		arr.push(newNodeB)
	}

	var lineArr2 = newNodeB.connectedEdges()
    
	var cline2 = findLine(lineArr2, clineData.id)


	arr.push(cline2.source())

	return arr
	// console.log("newNodeA:", newNodeA.connectedEdges(), cline)

}



function findLineArr (nodeA, line) {
	var arr = [];
	var edgeLine = cyMrg.findIdLine(line.id)
	var newNodeA = edgeLine.target()

	arr.push(edgeLine)

	var lineArr = newNodeA.connectedEdges()
    
	var cline = findLine(lineArr, line.id)

	var clineData = cline.data()
	arr.push(cline)
	var newNodeB = ''
	if (clineData.target == newNodeA.data().id) {
		newNodeB = cline.source()
	
	}else {
		newNodeB = cline.target()
		
	}

	var lineArr2 = newNodeB.connectedEdges()
    
	var cline2 = findLine(lineArr2, clineData.id)

    arr.push(cline2)

	return arr
	
}

export default {

	moveGroup () {

	},

	delNode (node) {
		console.log("del node:", node)
		var newArr = node.connectedEdges()
        for(var i=0; i< newArr.length ; i++) {
        	let line = newArr[i].data()
        	console.log("line :", line)
        	if(line.type == "containerLine") {
        		this.delLine(newArr[i])
        	}
        }

        dataC.cy.remove(node)
		
	},

	findLinkNode (node) {
		var allLine = node.connectedEdges()
		var node = []
		for(var i=0; i< allLine.length; i++) {
			let garr = findNodeArr(node, allLine[i].data())
			node.push(garr[3])
			if(garr[3].hasClass('eater')) {
				garr[3].removeClass('eater')
			}else {
				garr[3].addClass('eater')
			}
			
		}

		
	},

	
	fcline (line) {
		var lineData = line.data()
		var temA = ''
		var temB = ''
		var lineArr = []
		var sourceNode = ''
		if(lineData.pos == 'c') {
			temA = line.source()
			temB = line.target()

			var arr = temA.connectedEdges()
			var lineBgroup = temB.connectedEdges()

			var newLine = findLine(lineBgroup, lineData.id)

			
			for (var i=0; i< arr.length; i++) {
				lineArr.push(arr[i])

				console.log("lineNode:", arr[i].data())
			}
			
			lineArr.push(newLine)
			
		} else {
			sourceNode = line.source()
			lineArr = findLineArr(sourceNode, lineData)
		}

		return lineArr
	}, 

	delLine (line) {
		var lineData = line.data()
		var temA = ''
		var temB = ''
		var nodeArr = ''
		var sourceNode = ''
		if(lineData.pos == 'c') {
			temA = line.source()
			temB = line.target()
			//dataC.cy.remove(line.source)
		} else {
			sourceNode = line.source()
			nodeArr = findNodeArr(sourceNode, lineData)
			temA = nodeArr[1]
			temB = nodeArr[2]
		}

		dataC.cy.remove(temA)
		dataC.cy.remove(temB)
	},

	hasLine (nodeA, nodeB) {
		var idB = nodeB.data().id
		var lineArr = nodeA.connectedEdges()
		
		for(var i=0; i<lineArr.length; i++) {
			let edge = lineArr[i].data()
			
			if(edge.type == 'containerLine') {
				var nodeArr = findNodeArr(nodeA, edge)

				var lineAllArr = findLineArr(nodeA, edge)
				
				let tid = nodeArr[3].data().id

				if(tid == idB) {
					
					cyMrg.dhNode(nodeArr[1].data().id, 'visible')
					cyMrg.dhNode(nodeArr[2].data().id, 'visible')
					
					return true
				}

			}
		}

		return false;
		console.log("lineArr:", lineArr)

	},

	setGroupLineColor (node) {
		var linkColor = node.data().linkColor || "red"
		var lineGroup = node.connectedEdges()
		for(var i=0; i< lineGroup.length; i++) {
			let line = lineGroup[i].data()
			if(line.type == 'containerLine') {
				let lineArr = findLineArr(node, line)
				for(var j=0; j < lineArr.length; j++) {
					if(lineArr[j]) {
						lineArr[j].style({
							"line-color": linkColor
						})
					}
					
				}
			}
			
		}

	},

	lineGroup (nodeA, nodeB) {
		var AP = nodeA.position()
		var BP = nodeB.position()

		var nodeAData = nodeA.data()
		var nodeBData = nodeB.data()
		var linkColor = '#fc1204'
		
		if(nodeAData.type == 'container' && nodeBData.type == 'container') {
			linkColor  = '#fc1204'
		} else if(nodeBData.type == 'container') {
			linkColor = nodeBData.linkColor || '#fc1204'
		} else if (nodeAData.type == 'container') {
			linkColor = nodeAData.linkColor || '#fc1204'
		} 
		

        var pgroup = getPosition (nodeA, nodeB)
       
		var pgroup = getPosition (nodeA, nodeB)

		var edgeA = cyMrg.getNewEdges()

		var edgeB = cyMrg.getNewEdges()

		var edgeC = cyMrg.getNewEdges()

		edgeA.color = linkColor
		edgeB.color = linkColor
		edgeC.color = linkColor

		var newNodeA = cyMrg.getNewNode()

		var newNodeB = cyMrg.getNewNode()


		edgeA.source = nodeA.data().id

		edgeA.target = newNodeA.id

		edgeB.source = newNodeA.id

		edgeB.target = newNodeB.id

		edgeC.source = nodeB.data().id

		edgeC.target = newNodeB.id

		// edgeA['containerLine'] = true
		// edgeB['containerLine'] = true

		edgeA['type'] = "containerLine"
		edgeA['pos'] = "l"
		
		edgeB['type'] = "containerLine"
		edgeB['pos'] = "c"
		
		edgeC['type'] = "containerLine"
		edgeC['pos'] = "r"
		
		dataC.cy.add({
		          group: 'nodes',
		          data: newNodeA,
		          position: pgroup[0],
		          classes: "unbundled-bezier"
		    	}).style({
			        'shape': 'rectangle',
			        'width': '1px',
			        'zIndex': 100,
			        'height': '1px'
			      })

		dataC.cy.add({
		          group: 'nodes',
		          data: newNodeB,
		          position: pgroup[1],
		          classes: "unbundled-bezier"
		    	}).style({
			        'shape': 'rectangle',
			        'width': '1px',
			        'zIndex': 100,
			        'height': '1px'
			      })    	

		dataC.cy.add({
		          group: 'edges',
		          data: edgeA,
		          classes: "unbundled-bezier"
		    	})

		dataC.cy.add({
		          group: 'edges',
		          data: edgeB,
		          classes: "unbundled-bezier"
		    	})
		dataC.cy.add({
		          group: 'edges',
		          data: edgeC,
		          classes: "unbundled-bezier"
		    	})

		this.movePoint(nodeA, edgeA)

	},

	movePoint (nodeA, line) {
		
		//var edgeLine = cyMrg.findIdLine(line.id)
		var nodeArr = findNodeArr(nodeA, line)

		var pgroup = getPosition (nodeArr[0], nodeArr[3])

		var newNodeA = nodeArr[1]
		var newNodeB = nodeArr[2]

		newNodeA.position(pgroup[0])
		newNodeB.position(pgroup[1])

	   this.judge(nodeArr, pgroup, line)
	},

	jcenter (nodePos, flag , nodeA) {
		var startT = nodePos.startY - nodePos.startH/2
		var startB = nodePos.startY + nodePos.startH/2
		var endT = nodePos.endY - nodePos.endH/2
		var endB = nodePos.endY + nodePos.endH/2
		
        if(flag == 'top') {
        
        	var d = nodeA.data()
        	if(startB < endT  && d.type != 'container') {
				return true
			} else if(startT > endB  && d.type == 'container') {
				return true
			} else {
				return false
			}

        } else {
        	if(startB > endT && startT < endB) {
				return true
			} else {
				return false
			}
        }
		
	},

	judge (nodeArr, pgroup, line) {

		var temNode = ""

		var nodeA = nodeArr[0]
		var nodeB = nodeArr[1]
		var nodeC = nodeArr[2]
		var nodeD = nodeArr[3]

		var nodeApos = nodeA.position()
		var nodeDpos = nodeD.position()

		var nodePos = {
			startH: nodeA.height(),
			startW: nodeA.width(),
			startX: nodeApos.x,
			startY: nodeApos.y,
			endX: nodeDpos.x,
			endY: nodeDpos.y,
			endH: nodeD.height(),
			endW: nodeD.width()
		}
		
		if(nodePos.startX < nodePos.endX - nodePos.endW/2 && this.jcenter(nodePos)) {
		//if(startX < endX - endW/2 && startY < endY + endH/2 && startY > endY - endH/2) {
			
			var cwidth = nodePos.endX - nodePos.startX - nodePos.startW/2 - nodePos.endW/2

			var b = {
				x: nodePos.startX + cwidth/2 + nodePos.startW/2,
				y: nodePos.startY
			}

			var c = {
				x: nodePos.endX - cwidth/2 - nodePos.endW/2,
				y: nodePos.endY
			}

			nodeB.position(b)
			nodeC.position(c)

		} else if (nodePos.startX > nodePos.endX + nodePos.endW/2 && this.jcenter(nodePos)) {
			var cwidth = nodePos.startX - nodePos.endX - nodePos.startW/2 - nodePos.endW/2

			var b = {
				x: nodePos.startX - cwidth/2 - nodePos.startW/2,
				y: nodePos.startY
			}

			var c = {
				x: nodePos.endX + cwidth/2 + nodePos.endW/2,
				y: nodePos.endY
			}

			nodeB.position(b)
			nodeC.position(c)
		} else if (this.jcenter(nodePos, 'top', nodeA)) {
			var d = nodeA.data()
			var b = {
				x: nodePos.startX,
				y: nodePos.endY - nodePos.endH/2 - 100
			}

			var c = {
				x: nodePos.endX,
				y: nodePos.endY - nodePos.endH/2 - 100
			}

			if(!d.parent && d.type != 'container') {
				nodeB.position(b)
				nodeC.position(c)
			} else if(d.type == 'container') {
				b = {
					x: nodePos.startX,
					y: nodePos.startY - nodePos.startH/2 - 100
				}

				c = {
					x: nodePos.endX,
					y: nodePos.startY - nodePos.startH/2 - 100
				}
				nodeB.position(b)
				nodeC.position(c)
			}
		}

	},

	drag (el, moveXY) {

		var id = el.data().id
		var self = this
		//if (Config.dragLayer.num != -1 || true) {
			dataC.cy.nodes(function (i, node) {
				let pid = node.data().id
				if(node.hasClass('selected') && pid != id) {
					let position = node.position()
					let newp = {
						x: position.x + moveXY.mx,
						y: position.y + moveXY.my
					}
					
					node.position(newp)
					// self.drag_one(node)
				}
				
			})
		//} 
		//this.drag_one(el)
	},


	drag_one (el) {
		var id = el.data().id
		var self = this
		var lineBgroup = el.connectedEdges()
		for(var i=0; i<lineBgroup.length; i++) {
			var line = lineBgroup[i].data()
			if(line['type'] == "containerLine" && line['source'] == id){
				self.movePoint(el, line)
			}	
		}
		
	}
}